package command;

import model.impl.PassengerImpl;
import DTO.PassengerDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It checks that the received DTO of passenger from client user is correct
 * and currently is online and in case that it's true sends OK null, if it's 
 * not that it sends NO and null.
 */
public class ClientLogOut implements Command {
	
	private Protocol protocol = new Protocol();

	@Override
	public void execute(ReceivedData recived) {
		model.Passenger passengerImpl = new PassengerImpl();
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		PassengerDTO passengerDTO = (PassengerDTO)message.getObject();
		if (passengerImpl.logOutOfExistingPassenger(passengerDTO)) {
			recived.server.authorizedUsers.remove(recived.socket);
			Protocol.Message answer = this.protocol.new Message("ok", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}
	}

}
